home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / tcqbsnip.zip / INTUTOR.TXT < prev    next >
Text File  |  1997-06-19  |  13KB  |  315 lines

  1.  =================================
  2. [     QuickBasic 4.5 Tutorial     ]
  3. [      How To Use Interrupts      ]
  4. [ Copyright (c) 1996 by Tika Carr ]
  5.  =================================
  6. (Please read disclaimer at the end of this tutorial.)
  7.  
  8. This tutorial hopes to cover the basics of how to use Interrupts in
  9. QuickBasic. Note that this method only works in QuickBasic 4.5. I hope
  10. to do a tutorial for those who use QBasic (which comes with MS-DOS 5.0
  11. and higher).
  12.  
  13. 1. Getting Started
  14.  
  15. You will need to have started QuickBasic 4.5 by typing the following
  16. at the MS-DOS prompt:
  17.  
  18. QB /L QB.QLB
  19.  
  20. This QuickLiBrary will load in what you need to use interrupts.
  21.  
  22. Many new programmers avoid using interrupts because they are afraid to
  23. damage their computer. I've noticed people mess up their system
  24. *without* using interrupts. Sections 7 - 9 have some tips on safe
  25. debugging and what to do if you have a crash. Further,  BACK UP YOUR
  26. HARD DRIVES! This is MOST important! And save your programs onto
  27. floppy diskette before you run them. Ultimately, its still up to you
  28. to protect your system. This goes for any type of programming.
  29.  
  30. 2. Your First Interrupt
  31.  
  32. Type in the following and save it, then run it. We'll look at the
  33. program and see how it all works in a moment.
  34.  
  35. =======>8 Snip 8<=======
  36.  
  37. 'Example Program for CALL INTERRUPT Tutorial
  38. 'by Tika Carr
  39.  
  40. '$INCLUDE: 'QB.BI'
  41.  
  42. DIM Inregs AS RegType, Outregs AS RegType
  43.  
  44. 'Int 10h (interrupt 10 hexadecimal) controls the video part.
  45. '0Ah tells the computer to write a character on the screen.
  46. 'We'll put the letter 'A' on the screen in this example.
  47.  
  48. CLS
  49.  
  50. Inregs.ax = &HA41  'load high and low bytes into ax register (&H0A01)
  51. Inregs.cx = 1      'write only 1 character
  52.  
  53. CALL INTERRUPT(&H10, Inregs, Outregs) 'put the character on the screen
  54.  
  55. =======>8 Snip 8<=======
  56.  
  57. '$INCLUDE: 'QB.BI' defines the type structures used for the
  58. interrupts.
  59.  
  60. DIM Inregs AS RegType, Outregs AS RegType
  61.  
  62. The INCLUDE statement defines the type structures that is used for
  63. interrupts. These are found in the QB.BI file that comes with
  64. QuickBasic 4.5. The DIM Statement lets you specify what variable to
  65. put the registers defined in RegType in, so that its easy to pass all
  66. the registers to the interrupt.
  67.  
  68. 3. What are registers and what do they do?
  69.  
  70. A register is a place where you store values, and is a more direct way
  71. to communicate with the computer. The computer looks into registers
  72. for specific values, and uses them to perform different tasks. For
  73. example, we gave the computer some information in the AX register,
  74. telling it we wanted to write something on the screen, and what we
  75. wanted to write to the screen (the letter 'A'). We also put a value
  76. into the CX register, telling the computer we wanted to write only one
  77. copy of the letter 'A'. When you call an interrupt, you send all that
  78. information along to the computer (int 10h, which accesses your
  79. video). Basically, we just told the computer to PRINT "A" on the
  80. screen. Registers also let the computer send information back to your
  81. program. For example, INT 33 can give your program the X and Y
  82. coordinates of where the mouse currently is located.
  83.  
  84. For CALL INTERRUPT: ax, bx, cx, dx, bp, si, di, flags
  85. Defined as RegType
  86.  
  87. FOR CALL INTERRUPTX: ax, bx, cx, dx, bp, si, di, flags, ds, es
  88. Defined as RegTypeX
  89.  
  90. Depending on the interrupt you want to use, you will need to pick the
  91. type of call that suits it. For instance, if you don't use the es
  92. register, then using CALL INTERRUPT would work fine. However, if the
  93. computer will be looking into the es register for something, or if you
  94. will need to know what is in the ds register, you will want to use the
  95. CALL INTERRUPTX.
  96.  
  97. These definitions are all in the QB.BI file. You '$INCLUDE: 'QB.BI' in
  98. your program, then you DIM Inregs AS RegyType, OutRegs AS RegType.
  99. These will set up your variables so that you can access the registers.
  100.  
  101. To put something into the registers, you use Inregs, and to read the
  102. registers, you use the OutRegs variable:
  103.  
  104. Inregs.ax  is where you would put something in the AX register.
  105. Outregs.cx is where you can find what the computer put in the CX
  106.            register.
  107.  
  108. 4. Storing Values into Registers:
  109.  
  110. Since the registers take information in bytes only, you may have to do
  111. some converting to load the registers properly. Many times an
  112. interrupt listing will show something like:
  113.  
  114. Interrupt 10h: Video
  115.         Entry: ah = 0A write a character to the screen
  116.                al = value of character to write
  117.                bh = video page
  118.                bl = attribute or color of character
  119.                cx = number of times to write the character
  120.  
  121. This can seem confusing. How do you load the AX register? Where IS it?
  122. There's an AH and an AL. These mean the High and Low bytes of the AX
  123. register, respectively. Here's how you would put a value into the AX
  124. register:
  125.  
  126. Inregs.ax = &HA41  'load high and low bytes into ax register (&H0A01)
  127.  
  128. (Note that QuickBasic likes to take away the leading 0s. Initially, we
  129. typed it as: Inregs.ax = &H0A41)
  130.  
  131. The values go into the registers as:  0A41
  132.                                       HiLo
  133.  
  134. Most of the time, you probably will run into this situation:
  135.  
  136. Video$ = "0A"              ' Tell computer to write to video
  137. Character$ = "A"           ' Character to write on screen
  138.  
  139. Here is how you would get it all into one register:
  140.  
  141. Character$ = HEX$(ASC(A))  ' Convert 'A' into its ASCII value in
  142.                            ' Hexadecimal.
  143.  
  144. Since Video$ already is in Hexadecimal, we won't need to change it.
  145. Now, we put them together:
  146.  
  147. AX$ = Video$ + Character$      ' AX$ now contains 0A41 PRINT AX$ to
  148.                                 ' see for yourself.
  149.  
  150. Now that we got the full Hexadecimal value to put into the AX
  151. register, we still have to convert this into a *number*:
  152.  
  153. Inregs.ax = VAL("&H" + AX$)
  154.  
  155. This makes the string now say "&H0A41" and it also converts it into a
  156. numeric value (using VAL). Now you have the high and low bytes
  157. converted and stored into the AX register that will go into the
  158. computer (Inregs). When you do the CALL INTERRUPT (&H10, Inregs,
  159. Outregs), the values will be correctly loaded where the computer can
  160. find them.
  161.  
  162. 5. Reading the Registers
  163.  
  164. Outregs also holds register values. After you make a CALL INTERRUPT,
  165. you can read, let's say, the high and low bytes of the BX register and
  166. use it in your own program:
  167.  
  168. BX$ = HEX$(Outregs.bx)      ' Convert the value to Hexadecimal, its
  169.                             ' easier to extract the high and low bytes
  170.                             ' this way.
  171.  
  172. Since the computer likes to truncate leading 0s, we have to convert
  173. the value of BX$:
  174.  
  175. ' Get Low and High Byte of BX
  176. ' BH$ is the high byte, BL$ is the low byte, both in Hexadecimal.
  177.  
  178. L = LEN(BX$)
  179. IF L = 1 THEN BH$ = "0" + BX$: BL$ = "00"
  180. IF L = 2 THEN BH$ = LEFT$(BX$, 2): BL$ = "00"
  181. IF L = 3 THEN BH$ = "0" + LEFT$(BX$, 1): BL$ = RIGHT$(BX$, 2)
  182. IF L = 4 THEN BH$ = LEFT$(BX$, 1): BL$ = RIGHT$(BX$, 2)
  183.  
  184. bh = VAL("&H" + BH$)        ' Decimal Value of high byte of BX
  185. bl = VAL("&H" + BL$)        ' Decimal Value of low byte of BX
  186.  
  187. Note that this is only applicable for any register you need to get
  188. specifically the low and high bytes of. Sometimes a register is a
  189. pointer to a memory address. If that is the case, you can just use
  190. that value directly, without any type of conversion. For example:
  191.  
  192. Address = Outregs.es
  193. Value = Outregs.dx
  194. POKE Address, Value
  195. ' Or do whatever you need to with the address returned.
  196.  
  197. 6. Calling the Interrupt
  198.  
  199. When you do a CALL INTERRUPT you access a certain function within the
  200. computer. For example, in CALL INTERRUPT(&H10, Inregs, Outregs) you
  201. called the video interrupt 10h (&H10). Its best of course, to know
  202. what interrupt does what, what to put into the registers, and what the
  203. registers may return to your program that you may be able to use. The
  204. best Interrupt source I've found is Ralph Brown's Interrupt List,
  205. found on some programming BBSs and on the internet on different FTP
  206. sites (like Oakland, SimTel, and Garbo, which you can get current
  207. addresses for by searching Lycos at http://lycos.cs.cmu.edu)
  208.  
  209. 7. What To Do About Crashes
  210.  
  211. a) BACKUP YOUR PROGRAMS BEFORE RUNNING THEM!
  212. First and foremost, its good practice to save your program onto a
  213. floppy diskette before you run it. When your system crashes and you
  214. get back in, just reload the program into QuickBasic.
  215.  
  216. b) System Crashes
  217. If your system crashes, or seems to hang, first try hitting CTRL-C or
  218. CTRL-Pause (which is also CTRL-Break). You may have to hit ENTER
  219. afterward to get back to the QuickBasic Interactive Debugging Editor
  220. (IDE) to look at your program. If this don't work, reboot the computer
  221. with CTRL-ALT-DEL (or hit the RESET button on the computer if that
  222. didn't work). Then reload and take a look at your program. If worse
  223. comes to worse, you can shut off the computer, wait a few moments and
  224. turn it back on. Personally, I have always been able to recover by
  225. breaking out of the program with CTRL-Break.
  226.  
  227. c) Disk FAT crashes
  228. This is one situation that could occur if you are using interrupts to
  229. access the disk drives or hard disk, and you didn't get things loaded
  230. in right. Best to have your hard drive backed up before each
  231. programming session if you know you'll be using interrupts that will
  232. access the disk drives (ie. may have potential of writing to sectors
  233. or the FAT). Another good thing to have on hand is some utilities that
  234. repair damaged FAT tables and such. There are a number of good
  235. commercial programs out there, and some shareware ones as well. Put
  236. one of these on a bootable floppy.
  237.  
  238. d) Video, Sound and other hardware
  239. Its rare that you can actually damage hardware with an interrupt call.
  240. If something goes "haywire" the best bet is to just hit the reset
  241. button on the PC right away. Usually, things will then reset and
  242. recover.
  243.  
  244. 8. Safe Debugging
  245.  
  246. Once you get your program written, put a remark before the CALL
  247. INTERRUPT:
  248.  
  249. 'CALL INTERRUPT (&H10, Inregs, Outregs)
  250.  
  251. Then set up the Debug to watch your variables:
  252.  
  253. HEX$(Inregs.ax)
  254. HEX$(Outregs.bx)
  255.  
  256. Or whatever variables you are working with. Then ALT-R R to restart.
  257. NOW SAVE THE PROGRAM TO FLOPPY DISK! Remove the disk from the drive.
  258. Hit F8 to step through your program one instruction at a time, paying
  259. close attention to the values in the variables. Are they loading
  260. properly? Once you think its working, you can again save the program
  261. and then remove the remark from the call. Step through again and pay
  262. attention to the Outregs registers if you are using them.
  263.  
  264. It may seem like a lot to go through, but watching how your program
  265. works step by step, especially if you're first learning to use
  266. interrupts, will show you how the computer uses them, and how your
  267. programs behave (for better or for worse).
  268.  
  269. 9. In Closing....
  270.  
  271. Interrupts are a great way to do things in QuickBasic that you can't
  272. find a command for. Normally, they don't hurt anything and at worse,
  273. just make you have to restart the computer. While a risk is there to
  274. mess up things like hard drives, its rare you'll run into that, if at
  275. all, as long as you don't use disk interrupts until you are
  276. comfortable with how interrupts work and how to use them. Stick with
  277. writing for video, mouse, printer, sound card for starters. Video is
  278. easiest, as is the mouse. And if weird things happen, don't panic -
  279. reset. :)
  280.  
  281.                     ******* DISCLAIMER *******
  282.  
  283. The author of this article cannot gaurantee the usability or
  284. suitability of the information presented herein for any particular
  285. purpose. In addition, the user of the information in this article
  286. agrees not to hold the author, moderator or any other direct or
  287. indirect agent liable in any way for any damages, loss of data, or
  288. other consequences arising from use of this information. While I have
  289. made every conscious effort to ensure the information in this tutorial
  290. is accurate and safe to use on any PC compatible in the QuickBasic 4.5
  291. environment, the end result depends on the person making use of the
  292. information presented here. Use the information in this tutorial at
  293. your own risk.
  294.  
  295.                   ******* CONTACT INFORMATION *******
  296.  
  297. As of 8/3/96, comments, questions and suggestions, can be directed to:
  298.  
  299.  FidoNet: Tika Carr 1:2613/601 or 1:2613/313
  300. Internet: t.carr@pobox.com
  301.  
  302. =====================================================================
  303. Tika Carr, former staff writer and later editor of GEnieLamp PC
  304. Multimedia Magazine, has been writing QuickBasic 4.5 programs since
  305. 1989, and is a frequent contributor to the QUICK_BAS FidoNet Echo. Her
  306. area of specialty is in "tools that make tools" (Steven Levy,
  307. "Hackers"), meaning anything that will make things easier for
  308. programmers to take control of the computer, and make their
  309. imaginations come alive.
  310. =====================================================================
  311. Microsoft, QuickBasic 4.5, and QBasic are trademarks of Microsoft
  312. Corporation. MS-DOS is a registered trademark of Microsoft
  313. Corporation.
  314.  
  315.